import re
from time import sleep
import allure
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from common.log import log


@allure.step('鼠标左键点击')
def sel_click(drive, sel, timeout=20):
    """
    鼠标左键单击（元素中是否可见并且是enable的，代表可点击  参考：https://www.cnblogs.com/an5456/p/11279879.html）
    :param drive:浏览器驱动
    :param sel: selenium元素
    :param timeout: 超时时间，默认20s
    :return:
    """
    try:
        WebDriverWait(drive, timeout).until(expected_conditions.element_to_be_clickable((sel))).click()
        sleep(0.2)
        # 提取正则表达式的中文  比如"//a[contains(text(),'登录')]"中获取"登录"并点击
        selen = re.sub('[^\u4e00-\u9fa5]+', '', str(sel))
        if len(selen) > 0:
            log.info(f"点击：{selen}")
        return True
    except Exception as e:
        log.error(f"无法定位到该元素：{sel}，异常为：\n{e}")
        raise e


@allure.step('元素可点击')
def elements_clickable(drive, by, sel, timeout=30):  # element_visible
    """
    元素是否可点击
    :param drive:浏览器驱动
    :param by:定位方式 id、xpath等
    :param sel:selenium元素
    :param timeout:超时时间
    :return:
    """
    try:
        WebDriverWait(drive, timeout).until(expected_conditions.element_to_be_clickable((by, sel)))
        return True
    except Exception as e:
        log.error(f"已达到超时时间，元素【{sel}】仍未加载出来，不可点击，异常为{e}")
        raise e


@allure.step('元素可见')
def elements_visibility(drive, by, sel, timeout=30):
    """
    元素是否可见
    :param drive: 浏览器驱动
    :param by: 定位方式
    :param sel: selenium元素
    :param timeout: 超时时间
    :return:
    """
    try:
        WebDriverWait(drive, timeout).until(expected_conditions.visibility_of_element_located((by, sel)))
        return True
    except Exception as e:
        log.error(f"已达到超时时间，元素【{sel}】仍不可见，异常为{e}")
        raise e


@allure.step('输入内容')
def sel_send_keys(drive, sel, value, timeout=10):
    """
    输入内容
    :param drive:浏览器驱动
    :param sel:selenium元素
    :param value:输入的文本内容
    :param timeout:超时时间
    :return:
    """
    try:
        WebDriverWait(drive, timeout).until(expected_conditions.element_to_be_clickable((sel))).clear()
        sleep(0.2)
        WebDriverWait(drive, timeout).until(expected_conditions.element_to_be_clickable((sel))).send_keys(value)
        sleep(0.2)
        selen = re.sub('[^\u4e00-\u9fa5]+', '', str(sel))
        if len(selen) > 0:
            log.info(f"点击：{selen}，并输入值{value}")
    except Exception as e:
        log.error(f"无法定位到元素：{sel},异常为：\n{e}")
        raise e


@allure.step('获取指定元素的Text值')
def get_text(driver, els, timeout=10, mode=0):
    """
    获取指定元素的text值
    :param driver: 浏览器驱动
    :param els: element元素
    :param timeout: 超时时间
    :param mode:
    :return:
    """
    try:
        # 等待元素在页面上出现
        element = WebDriverWait(driver, timeout).until(EC.presence_of_element_located((els)))
        if mode == 0:
            log.info(element.text)
            return element.text
        elif mode == 1:
            log.info(element.get_attribute('textContent'))
            return element.get_attribute('textContent')
    except Exception as e:
        log.error(f"获取元素Text报错，{e}")
        return None
